package com.io.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 文件复制
 * 
 * @author Administrator
 * 
 */
public class ChannelCopy {

	private static final int BSIZE = 1024;

	public static void main(String[] args) throws IOException {
		FileChannel in = new FileInputStream("c:/vcredist_x86.log")
				.getChannel(), out = new FileOutputStream(
				"c:/vcredist_x862.log").getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
		while (in.read(buffer) != -1) {
			buffer.flip();
			out.write(buffer);
			// clear操作对所有的内部指针重新安排，以便缓冲器在另一个read操作期间能够做好接受数据的准备
			buffer.clear();
		}
	}

}
